/*     */ package com.zimbra.cs.servlet;
/*     */ 
/*     */ import com.zimbra.common.account.Key.DomainBy;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.util.HttpUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.soap.SoapProvisioning;
/*     */ import java.io.IOException;
/*     */ import java.util.concurrent.ConcurrentHashMap;
/*     */ import java.util.concurrent.ConcurrentMap;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import javax.servlet.Filter;
/*     */ import javax.servlet.FilterChain;
/*     */ import javax.servlet.FilterConfig;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.ServletRequest;
/*     */ import javax.servlet.ServletResponse;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SetHeaderFilter
/*     */   implements Filter
/*     */ {
/*  45 */   private static final Log LOG = ZimbraLog.misc;
/*  46 */   private static final KeyValue[] NO_HEADERS = new KeyValue[0];
/*  47 */   private static final ConcurrentMap<String, KeyValue[]> RESPONSE_HEADERS = new ConcurrentHashMap();
/*     */   
/*     */   public static final String P_RESPONSE_HEADERS_ENABLED = "zimbraResponseHeader.enabled";
/*  50 */   public static final Pattern RE_HEADER = Pattern.compile("^([^:]+):\\s+(.*)$");
/*     */   public static final String UNKNOWN_HEADER_NAME = "X-Zimbra-Unknown-Header";
/*     */   
/*  53 */   public SetHeaderFilter() { this.isResponseHeadersEnabled = true; }
/*     */   
/*     */   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
/*     */     throws IOException, ServletException
/*     */   {
/*  58 */     if (doFilter(request, response)) {
/*  59 */       chain.doFilter(request, response);
/*     */     }
/*     */   }
/*     */   
/*     */   public void init(FilterConfig filterConfig) throws ServletException
/*     */   {
/*  65 */     String s = filterConfig.getInitParameter("zimbraResponseHeader.enabled");
/*  66 */     if (s != null) {
/*  67 */       this.isResponseHeadersEnabled = Boolean.parseBoolean(s.trim().toLowerCase());
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private boolean isResponseHeadersEnabled;
/*     */   
/*     */ 
/*     */   public void destroy() {}
/*     */   
/*     */ 
/*     */   public boolean doFilter(ServletRequest request, ServletResponse response)
/*     */     throws IOException, ServletException
/*     */   {
/*  82 */     addZimbraResponseHeaders(request, response);
/*  83 */     return true;
/*     */   }
/*     */   
/*     */   private void addZimbraResponseHeaders(ServletRequest request, ServletResponse response) {
/*  87 */     if (!this.isResponseHeadersEnabled) {
/*  88 */       return;
/*     */     }
/*  90 */     HttpServletRequest httpRequest = (HttpServletRequest)request;
/*  91 */     if (httpRequest.getRequestURI().startsWith("/service/admin/soap")) {
/*  92 */       return;
/*     */     }
/*  94 */     HttpServletResponse httpResponse = (HttpServletResponse)response;
/*  95 */     String serverName = HttpUtil.getVirtualHost(httpRequest);
/*  96 */     KeyValue[] headers = getResponseHeaders(serverName);
/*  97 */     addHeaders(httpResponse, headers);
/*     */   }
/*     */   
/*     */   private KeyValue[] getResponseHeaders(String serverName) {
/* 101 */     KeyValue[] headers = (KeyValue[])RESPONSE_HEADERS.get(serverName);
/* 102 */     if (headers == null) {
/* 103 */       headers = NO_HEADERS;
/*     */       try {
/* 105 */         SoapProvisioning provisioning = new SoapProvisioning();
/* 106 */         String soapUri = LC.zimbra_admin_service_scheme.value() + LC.zimbra_zmprov_default_soap_server.value() + ':' + LC.zimbra_admin_service_port.intValue() + "/service/admin/soap/";
/*     */         
/* 108 */         provisioning.soapSetURI(soapUri);
/* 109 */         provisioning.soapZimbraAdminAuthenticate();
/* 110 */         Entry info = provisioning.getDomainInfo(Key.DomainBy.virtualHostname, serverName);
/* 111 */         if (info == null) {
/* 112 */           info = provisioning.getConfig();
/*     */         }
/* 114 */         if (info != null) {
/* 115 */           String[] values = info.getMultiAttr("zimbraResponseHeader", true);
/* 116 */           headers = new KeyValue[values.length];
/* 117 */           for (int i = 0; i < values.length; i++) {
/* 118 */             String value = values[i];
/* 119 */             Matcher matcher = RE_HEADER.matcher(value);
/* 120 */             if (matcher.matches()) {
/* 121 */               headers[i] = new KeyValue(matcher.group(1), matcher.group(2));
/*     */             } else {
/* 123 */               headers[i] = new KeyValue(value);
/*     */             }
/*     */           }
/*     */         }
/*     */       } catch (Exception e) {
/* 128 */         getLogger().error("Unable to get domain config", e);
/*     */       }
/* 130 */       RESPONSE_HEADERS.putIfAbsent(serverName, headers);
/*     */     }
/* 132 */     return headers;
/*     */   }
/*     */   
/*     */   private void addHeaders(HttpServletResponse response, KeyValue[] headers) {
/* 136 */     if (headers == null) {
/* 137 */       return;
/*     */     }
/* 139 */     for (KeyValue header : headers) {
/* 140 */       addHeader(response, header);
/*     */     }
/*     */   }
/*     */   
/*     */   private void addHeader(HttpServletResponse response, KeyValue header) {
/* 145 */     response.addHeader(header.key, header.value);
/*     */   }
/*     */   
/*     */   protected Log getLogger() {
/* 149 */     return LOG;
/*     */   }
/*     */   
/*     */   private static final class KeyValue {
/*     */     public final String key;
/*     */     public final String value;
/*     */     
/*     */     public KeyValue(String value) {
/* 157 */       this("X-Zimbra-Unknown-Header", value);
/*     */     }
/*     */     
/*     */     public KeyValue(String key, String value) {
/* 161 */       this.key = key;
/* 162 */       this.value = value;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/servlet/SetHeaderFilter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */